import * as types from "../types/quickOrderTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import { isEmpty } from "../utils/utils";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";
import RegexUtil from "../utils/RegexUtil";

export function quickOrder(realName, mobile, area, callback) {
  return dispatch => {
    if (isEmpty(realName)) {
      toastShort("请输入联系人姓名");
      return;
    }
    if (isEmpty(mobile)) {
      toastShort("请输入联系人手机号");
      return;
    }
    if (!RegexUtil.checkMobile(mobile)) {
      toastShort("请输入正确的手机号");
      return;
    }
    if (isEmpty(area)) {
      toastShort("请输入详细地址");
      return;
    }
    dispatch({ type: types.QUICKORDER_LOADING });
    const params = {
      real_name: realName,
      cell_phone: mobile,
      address: area,
    };
    NetWorkUtil.post(ApiConstants.QUICK_ORDER, params)
      .then(res => {
        toastShort("预约成功请等待平台与你联系！");
        callback(res);
        dispatch({ type: types.QUICKORDER_DONE });
      })
      .catch(error => {
        toastShort(error.message);
        dispatch({ type: types.QUICKORDER_ERROR, error });
      });
  };
}

export function inputName(realName) {
  return dispatch => {
    dispatch({ type: types.QUICKORDER_CHANGE_CONTRACTOR, realName });
  };
}

export function inputMobile(mobile) {
  return dispatch => {
    dispatch({ type: types.QUICKORDER_CHANGE_MOBILE, mobile });
  };
}

export function inputAddress(area) {
  return dispatch => {
    dispatch({ type: types.QUICKORDER_CHANGE_ADDRESS, area });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.QUICKORDER_UNMOUNT });
  };
}
